/**
 * 
 */
package truerefactor.genetic;

import java.util.Comparator;

/**
 * GASmell - Class to contain the individual and total smell counts for an
 * individual member of a population from the Genetic Algorithm.
 * 
 * @author Isaac Griffith
 */
public class GASmell implements Comparator<GASmell> {

    /**
     * array of individual code smell values
     */
    private double individualVals[];

    /**
     * current total sum of all individual values
     */
    private double total = 0;
    /**
     * current index for the next smell value to be added to the array of
     * individual values
     */
    private int index = 0;
    /**
     * max number of individual values that can be added
     */
    private int maxIndex;

    /**
     * Creates a new GASmell object to contain the code smell measurements
     * associated with an individual.
     * 
     * @param maxIndex
     *            The maximum amount of individual smell values to contain.
     */
    public GASmell(int maxIndex)
    {
        individualVals = new double[maxIndex];
        this.maxIndex = maxIndex;
    }

    /**
     * @return The array of individual values
     */
    public double[] getIndividualVals()
    {
        return individualVals;
    }

    /**
     * @param value
     *            value to be added to the array at the next index
     */
    public void addIndividualVal(double value)
    {
        if (index < individualVals.length)
        {
            individualVals[index % maxIndex] = value;
            total += value;
            index++;
        }
    }

    /**
     * @return the summation of all the individual values
     */
    public double getTotal()
    {
        return total;
    }

    /*
     * (non-Javadoc)
     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
     */
    @Override
    public int compare(GASmell arg0, GASmell arg1)
    {
        if (arg0.getTotal() > arg1.getTotal())
        {
            return 1;
        }
        else if (arg0.getTotal() < arg1.getTotal())
        {
            return -1;
        }
        else
        {
            return 0;
        }
    }
}
